package lumis.service.wsrp.consumer.doui;

import lumis.doui.control.Control;
import lumis.portal.PortalException;
import lumis.portal.UnexpectedException;
import lumis.service.wsrp.common.WSRPVersionFromTo;
import lumis.service.wsrp.consumer.producermanager.ProducerEntityManagerFactory;
import lumis.util.XmlUtil;

import com.sun.portal.wsrp.consumer.common.WSRPConsumerException;
import com.sun.portal.wsrp.consumer.producermanager.ProducerEntity;
import com.sun.portal.wsrp.consumer.producermanager.ProducerEntityManager;

/**
 * {@link Control} that adds subcontrols to its interface according with the
 * current service instance situation. If it's already configured then this
 * control renders controls to ask user if he wants or not to delete current
 * configuration and create a brand new one. In case user chooses to delete
 * current configuration, a process action handler is called to do so. If user
 * chooses not to delete current configuration, the popup is closed. If the
 * service instance isn't configured yet, an input text is shown so that the
 * user can enter the WSRP producer's WSDL address. After enter the correct
 * address, the user is taken to other interface.
 * 
 * @version $Revision: 13021 $ $Date: 2011-05-20 14:12:00 -0300 (Fri, 20 May 2011) $
 * @since 5.5.0
 */
public class ConsumerConfigurationInterfaceVerificationControl extends Control
{
	@Override
	public void buildSubControls() throws PortalException
	{
		super.buildSubControls();

		
		// verify if current service instance is already configured
		String serviceInstanceId = douiContext.getRequest().getServiceInstanceConfig().getId();
		ProducerEntityManager producerEntityManager = ProducerEntityManagerFactory.getProducerEntityManager(serviceInstanceId);
		boolean hasBeenConfigured;
		try
		{
			hasBeenConfigured = !(producerEntityManager.getProducerEntityIds().isEmpty());
		}
		catch (WSRPConsumerException e)
		{
			throw new UnexpectedException(e);
		}
		StringBuilder subControls = new StringBuilder();
		subControls.append("	<control type=\"lum_propertyPage\" renderRequiredValuesDescriptor=\"" + !hasBeenConfigured + "\" >\n");
		subControls.append("		<control type=\"lum_controlGroup\" style=\"height:130px;width:765px;overflow:auto;\">\n");
		// if it's already configured, we display a question asking user if he
		// wants or not to delete current configuration
		if (hasBeenConfigured)
		{
			try
			{
				ProducerEntity producerEntity = producerEntityManager.getProducerEntity(producerEntityManager.getProducerEntityIds().iterator().next());
				String version = producerEntity.getVersion().value();
				
				WSRPVersionFromTo fromTo = WSRPVersionFromTo.getFromValue(version);
				if (fromTo != null)
				{
					version = localize(fromTo.getLocalizableName());
				}
				subControls.append("			<control type=\"lum_inputHidden\" id=\"doDeletion\" value=\"true\" />\n");
				subControls.append("			<control type=\"lum_table\" >\n");
				subControls.append("				<tr>\n");
				subControls.append("					<td colspan=\"2\" style=\"font-weight:bold;\">\n");
				subControls.append("						<control type=\"lum_text\" text=\"STR_CONFIGURATION_EXISTS\" />\n");
				subControls.append("					</td>\n");
				subControls.append("				</tr>\n");
				subControls.append("				<tr>\n");
				subControls.append("					<td>\n");
				subControls.append("						<control type=\"lum_label\" text=\"STR_WSRP_WSDL_URL\" />\n");
				subControls.append("					</td>\n");
				subControls.append("					<td>\n");
				subControls.append("						<control type=\"lum_text\" text=\"" + XmlUtil.encodeXml(producerEntity.getURL().toString())
						+ "\" />\n");
				subControls.append("					</td>\n");
				subControls.append("				</tr>\n");
				
				subControls.append("				<tr>\n");
				subControls.append("					<td>\n");
				subControls.append("						<control type=\"lum_label\" text=\"STR_WSRP_VERSION\" />\n");
				subControls.append("					</td>\n");
				subControls.append("					<td>\n");
				subControls.append("						<control type=\"lum_text\" text=\"" + XmlUtil.encodeXml(version) + "\" />\n");
				subControls.append("					</td>\n");
				subControls.append("				</tr>\n");
				
				if (producerEntity.getRegistrationContext() != null)
				{
					subControls.append("				<tr>\n");
					subControls.append("					<td>\n");
					subControls.append("						<control type=\"lum_label\" text=\"STR_WSRP_REGISTRATION_HANDLE\" />\n");
					subControls.append("					</td>\n");
					subControls.append("					<td>\n");
					subControls.append("						<control type=\"lum_text\" text=\"" + XmlUtil.encodeXml(producerEntity.getRegistrationContext().getRegistrationHandle()) + "\" />\n");
					subControls.append("					</td>\n");
					subControls.append("				</tr>\n");


					// lifetime nao eh mostrado para quem nao tem registro
					if ((producerEntity.getRegistrationContext().getRegistrationHandle() != null)
							&& (!producerEntity.getRegistrationContext().getRegistrationHandle().equals("")))
					{
						//Date lifetime = WSRPUtils.dateFromXMLGregorianCalendar(producerEntity.getLifetime().getTerminationTime());
						
						
						subControls.append("				<tr>\n");
						subControls.append("					<td>\n");
						subControls.append("						<control type=\"lum_label\" text=\"STR_WSRP_LIFETIME\" />\n");
						subControls.append("					</td>\n");				
						subControls.append("					<td>\n");
						subControls.append("						<control type=\"lum_dateTimePicker\" id=\"lifetime\" fieldName=\"STR_WSRP_LIFETIME\" >\n");
						//subControls.append("							<control type=\"lum_requiredValueValidator\" />\n");
						subControls.append("						</control>\n");
						subControls.append("						<control type=\"lum_okButton\" id=\"configureRegistration\">\n");
						subControls.append("							<onClick processActionId=\"configureRegistration\" type=\"processAction\"/>\n");
						subControls.append("						</control>\n");
						subControls.append("					</td>\n");
						subControls.append("				</tr>\n");						
					}				
				}
				
				subControls.append("			</control>");
			}
			catch (WSRPConsumerException e)
			{
				throw new UnexpectedException(e);
			}
		}

		// else we show an input text so that user can enter he's WSRP
		// producer's WSDL address.
		else
		{
			subControls.append("			<control type=\"lum_inputHidden\" id=\"goConfig2\" value=\"true\" />\n");
			subControls.append("			<control type=\"lum_table\">\n");
			subControls.append("				<tr>\n");
			subControls.append("					<td>\n");
			subControls.append("						<control type=\"lum_label\" text=\"STR_WSRP_WSDL_URL\" showRequired=\"true\" />\n");
			subControls.append("					</td>\n");
			subControls.append("					<td>\n");
			subControls.append("						<control type=\"lum_inputText\" id=\"wsdl\" fieldName=\"STR_WSRP_WSDL_URL\" style=\"width:600px\" >\n");
			subControls.append("							<control type=\"lum_requiredValueValidator\" />\n");
			subControls.append("						</control>\n");
			subControls.append("					</td>\n");
			subControls.append("				</tr>\n");
			subControls.append("				<tr>\n");
			subControls.append("					<td>\n");
			subControls.append("						<control type=\"lum_label\" text=\"STR_WSRP_VERSION\" showRequired=\"false\" />\n");
			subControls.append("					</td>\n");
			subControls.append("				</tr>\n");
			subControls.append("			</control>\n");
		}
		
		subControls.append("		</control>/n");
		
		subControls.append("	</control>");
		XmlUtil.addNodeFromXmlString(controlDefinitionNode.getParentNode(), subControls.toString());

		// we can delete this control from interface. it has already done it's
		// work.
		controlDefinitionNode.getParentNode().removeChild(controlDefinitionNode);
	}
}
